package com.example.springboot.demo.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.AjaxResult;
import com.example.springboot.demo.entity.User;
import com.example.springboot.demo.entity.Vendor;
import com.example.springboot.demo.service.ExportService;
import com.example.springboot.demo.service.impl.UserServiceImpl;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("user")
public class UserController {

    //    @Resource
//    private UserService userService;
    @Resource
    private UserServiceImpl userService;

    @PostMapping("update")
    public AjaxResult updateUser(@RequestBody User user) {
        if (userService.update(user) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @GetMapping("/page")
    public IPage<User> listUser(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") String username,
                                @RequestParam(defaultValue = "") String email,
                                @RequestParam(defaultValue = "") String address) {
        IPage<User> page = new Page<>(pageNum, pageSize);
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("username", username);
        userQueryWrapper.like("email", email);
        userQueryWrapper.like("address", address);
        return userService.page(page, userQueryWrapper);
    }

    @GetMapping("/deleteUser")
    public AjaxResult deleteUser(@RequestParam("id") int id) {
        if (userService.deleteUser(id) > 0) {
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }

    @PostMapping("/count")
    public int countUser() {
        return userService.countUser();
    }

    @Resource
    private ExportService exportVentor;

    @PostMapping("vendor")
    public void listRelation() throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader("D:/text/vendor.json.txt"))) {
            String jsonStr = br.readLine();
            JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
//            JSONArray jsonArray = new JSONArray("[{\"id\":248,\"name_en\":\"AIS\",\"introduction\":1,\"definedStr\":\"系统预定义\",\"contact\":\"\",\"phone_number\":\"\",\"desc\":\"\"},{\"id\":247,\"name_en\":\"DZEC\",\"introduction\":1,\"definedStr\":\"系统预定义\",\"contact\":\"\",\"phone_number\":\"\",\"desc\":\"\"}]");
            Gson gson = new Gson();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Vendor ventor = gson.fromJson(jsonObject.toString(), Vendor.class);
                UUID uuid = UUID.randomUUID();
                // 截取UUID的前16位作为随机数
                String substring = uuid.toString().substring(0, 16);
                ventor.setId((long) Math.abs(UUID.randomUUID().hashCode()));
                ventor.setCreate_user(181009439703171072L);
                if (exportVentor.exportVentor(ventor) > 0) {
                    System.out.println(ventor.getId() + "导入成功");
                }
            }
        } finally {
            System.out.println("读取完毕");
        }
    }


}
